<?php
class onermbModel extends RPHP_Model
{

    //一元拍.九云币资料
    public function getPatCoins()
    {
       
        //获取用户信息
        $member = $this->getMemberInfo();
        //获取用户资金信息
        $member_money = $this->getMemberMoney();
        
        //获取用户累计冻结云米总额
        $sql     = "SELECT SUM(`money`) FROM {$this->_prefix['primary']}member_cloud_detail WHERE `uid`=".$this->_user['uid'];
        $cloud_total_price = $this->db->getone($sql);
        
        //获取一元拍正在拍/我拍中数量
        $sql = "SELECT * FROM {$this->_prefix['primary']}order_bidrecord WHERE `status`=1  AND `pay_status`=0 AND `uid`=".$this->_user['uid']." ORDER BY `create_time` DESC";
        $pats  = $this->sql($sql);
        $pat_onpay_num = 0;//超时弃权数量
        $pat_seccuss_num = 0;//我拍中数量
        $pat_being_num = 0;//正在拍数量
        foreach($pats as $val){
           // if($val['status']==1){
                if(time()<$val['pay_down_time']){
                    
                    $pat_seccuss_num  +=1;
                }else{
                    $pat_onpay_num +=1;
                }
                  
        
            
        }
        $arr = $this->getPatBeging();
        $pat_being_num = $arr['num'];
        //获取一元拍超时弃权数量
       /*  $sql     = "SELECT COUNT(*) FROM {$this->_prefix['primary']}order_info WHERE `uid`=".$this->_user['uid']." AND `sell_type`=4 AND `pay_status`=0";
        $pat_onpay_num = $this->db->getone($sql); */
        
        $arr =array();
        $arr['pat_onpay_num'] = $pat_onpay_num;
        $arr['pat_being_num'] = $pat_being_num;
        $arr['pat_seccuss_num'] = $pat_seccuss_num;
        $arr['cloud_total_price'] = $cloud_total_price;
        $arr['member_money'] = $member_money;
        $arr['member'] = $member;
        return  $arr;
        
    }
    //计算云米挥拍次数
    public function getUncolud()
    {
        
        $member = $this->getMemberMoney();
        $num = 10;//达标挥拍次数
        if($member['cloud_money']>0){
            
            $config = $this->getSetting();
          
            $sql  = "SELECT `create_time` FROM {$this->_prefix['primary']}member_cloud_detail  WHERE `uid`=" . $this->_user['uid']."  ORDER BY `create_time` DESC";
            $mtime = $this->db->getone($sql);
            $t = 0;
            if($mtime>0){
                $t = $mtime;
                
                
            }else{
                $sql  = "SELECT `create_time` FROM {$this->_prefix['primary']}order_cloud_detail  WHERE `uid`=" . $this->_user['uid']."  ORDER BY `create_time` DESC";
                $otime = $this->db->getone($sql);
                
                if($otime){
                    
                    $t = $otime;
                }
               
            }
           
            $sql  = "SELECT COUNT(*) FROM {$this->_prefix['primary']}order_cloud_detail  WHERE `uid`=" . $this->_user['uid'];
            $my_pat_num = $this->db->getone($sql);
              //计算达标挥拍次数
          
              $num = $config['freeze_num_pat'] * ceil((time() - $t)/(60*60*24));
        }
        $m = $this->getMemberInfo();
        $member['my_pat_num'] = $m['pat_num'];
        $member['pat_num'] = $num;
        return $member;
    }
    
    //解冻云米
    public function saveUncloud()
    {
        $arr = array();
        $arr['code'] = 900;
        $arr['msg'] = "云米解冻申请失败，请重试";
        $data = $this->getUncolud();
        if($data['cloud_money']>0){
            if($data['my_pat_num']>=$data['pat_num']){
                
                $sql = "INSERT INTO {$this->_prefix['primary']}member_cloud_detail
                                (`uid`,`money`,`create_time`) VALUES
	                            (" . $this->_user['uid'] . ",'" .$data['cloud_money']. "',". $_SERVER['REQUEST_TIME'].")";
                $rd = $this->sql($sql);
                if ($rd > 0) {
                
                    $arr['code'] = 200;
                    $arr['msg'] = "申请提交成功";
                }
                
            }else{
                $arr['code'] = 902;
                $arr['msg'] = "挥拍次数未达标，申请提交失败";
               
            }
            
        }else{
            $arr['code'] = 901;
            $arr['msg'] = "解冻云米金额必须大于0元";
            
        }
        
        return $arr;
        
    }
    //获取九云币明细
    public function getCoinsRecord($seach)
    {
        if($seach>0){
            $where=" AND `event`=".$seach;
            
        }else{
            
            $where=" AND `event`=12";
        }
       
        $sql     = "SELECT * FROM {$this->_prefix['primary']}finance_coins WHERE `uid`=".$this->_user['uid'].$where;
        $data = $this->sql($sql);
        return $data;
    }
    //九云币充值
    public function saveCoinsMoney($da)
    {
        $arr = array();
        $arr['code'] = 900;
       
        $money = (intval($da['money']));

        if($money){
            if(is_numeric($money)){
                //九云币消费变动
                   
                    $sql         = "INSERT INTO {$this->_prefix['primary']}coins_recharge (`uid`,`money`,`create_time`) VALUES (" . $this->_user['uid'] . ",". $money . "," . $_SERVER['REQUEST_TIME'] . ")";
                    $detail_id   = $this->sql($sql);
                   
                    if($detail_id){
                        $arr['code'] = 200;
                        $arr['id'] =$detail_id;
                        $arr['total_money'] =ceil($money * 100);;
                        $arr['money'] =$money;
                    }else{
                        
                        $arr['code'] = 903; 
                    }
                
               
                
            }else{
                $arr['code'] = 902;
             
                
            }
            
        }else{
            $arr['code'] = 901;
           
            
        }
        
        return  $arr;
        
    }
    
    public function getCoinsPayStatus($id)
    {
        $sql     = "SELECT COUNT(*) FROM {$this->_prefix['primary']}coins_recharge WHERE `id`=".$id." AND `pay_status`=0 AND `pay_time` =0";
        $num = $this->db->getone($sql);
        return $num;
    }
    //获取补偿明细
    public function getCompenRecord()
    {
        $sql     = "SELECT  `equalize_price`,`id`,`create_time` FROM {$this->_prefix['primary']}order_bidrecord WHERE `uid`=".$this->_user['uid'];
        $data = $this->sql($sql);
        return $data;
    }
    
    //我的银行列表
    public function myBankList()
    {
        $sql  = "select * from {$this->_prefix['primary']}member_bank_card where uid=" . $this->_user['uid'];
        $list = $this->sql($sql);
        foreach ($list as $k => $v) {
            $list[$k]['bank_name'] = $this->c['bank'][$v['bank_id']];
    
        }
    
        return $list;
    }
   
	//获取一元拍用户数据
	public function getOneRmbBuyData()
	{
		$uid = $this->_user['uid'];

        $sql  = "SELECT MA.*,FB.`coins_money` FROM {$this->_prefix['primary']}member_account MA LEFT JOIN {$this->_prefix['primary']}finance_balance FB ON FB.`uid`=MA.`uid` WHERE `uid`=" . $uid;
        $data = $this->db->getrow($sql);
        return $data;
	}
	
	//获取一元拍商品详情
	public function getOnePatGoods($goods_id)
	{
	    
	    $sql  = "SELECT * FROM {$this->_prefix['primary']}goods_data  WHERE `goods_id`=" . $goods_id." AND `is_delete`=0 AND `pat_count_down` >".time();
	    $data = $this->db->getrow($sql);
	    if($data){
	        $data['pat_total_min']   = ($data['pat_count_down'] - $data['pat_start_time'])/60;
	        
	    }
	    return $data;
	    
	}
	
	public function getPatGoodsList($time)
	{
	    $time -=70; //每一分钟执行一次
	    $sql  = "SELECT `goods_id`,`pat_start_time`,`is_pat`,`pat_max_price`,`pat_min_price`,`pat_fixed_price`,`pat_guarantee_price`,`pat_count_down`,`pat_order_count_down`,`pat_period_day`,`pat_start_time`,`is_risk`,`risk_floor_price`,`risk_frequency`,`pat_repeat_num` FROM {$this->_prefix['primary']}goods_data  WHERE `is_delete`=0  AND `pat_start_time` <=".$time." AND `pat_count_down`>".$time." AND `is_pat`=1";
	    $data = $this->sql($sql);
	    return $data;
	}
	    //获取出价记录
	    public function getPatRecord($goods_id)
	    {
	        $sql  = "SELECT `pat_start_time`,`pat_count_down` FROM {$this->_prefix['primary']}goods_data  WHERE `goods_id`=" . $goods_id." AND `is_delete`=0 AND `pat_count_down` >".time();
	        $goodsDetail = $this->db->getrow($sql);
	        
	        $sql  = "SELECT * FROM {$this->_prefix['primary']}order_bidrecord  WHERE `goods_id` = ".$goods_id ." AND `create_time`>=".$goodsDetail['pat_start_time']." AND `create_time`<".$goodsDetail['pat_count_down']." ORDER BY `bid_price` DESC,`create_time` DESC LIMIT 2";
	        $data = $this->sql($sql);
	        if($data){
    	        foreach($data as $k=>$v){
    	            
    	            $sql  = "SELECT `realname` FROM {$this->_prefix['primary']}member_account WHERE `uid`=" . $v['uid'];
    	            $name = $this->db->getone($sql);
    	            if($name){
    	               $data[$k]['name'] =$name;
	                }else{
	                    $data[$k]['name'] ="匿名用户";
	                    
	                }
    	        }
	        }
	        return $data;
	        
	    }
	    
	    //获取出价记录
	    public function getPatRecords($goods_id)
	    {
	        
	        $sql  = "SELECT `pat_start_time`,`pat_count_down` FROM {$this->_prefix['primary']}goods_data  WHERE `goods_id`=" . $goods_id." AND `is_delete`=0 AND `pat_start_time` <".time();
	        $goodsDetail = $this->db->getrow($sql);
	      
	        $sql  = "SELECT * FROM {$this->_prefix['primary']}order_bidrecord  WHERE `goods_id` = ".$goods_id ." AND `create_time`>=".$goodsDetail['pat_start_time']." AND `create_time`<".$goodsDetail['pat_count_down']." ORDER BY `bid_price` DESC,`create_time` DESC";
	        $data = $this->sql($sql);
	        if($data){
	            foreach($data as $k=>$v){
	                 
	                $sql  = "SELECT `realname` FROM {$this->_prefix['primary']}member_account WHERE `uid`=" . $v['uid'];
	                $name = $this->db->getone($sql);
	                
	                if($name){
    	               $data[$k]['name'] =$name;
	                }else{
	                    $data[$k]['name'] ="匿名用户";
	                    
	                }
	                
	            }
	        }
	        return $data;
	         
	    }
	    //拍买商品
	    public function savePatBuy($da)
	    {
	        $arr = array();
	        $arr['code'] = 900;
	        $arr['msg'] = "购买失败，请重试！";
	        $goods_id = intval($da['goods_id']);
	        if($goods_id){
	            //查出商品详情
	            $goodsDetail = $this->getOnePatGoods($goods_id);
	        
	            if($goodsDetail){
	                //查看是否开始拍卖
	                if(time() >= $goodsDetail['pat_start_time'] && time()<$goodsDetail['pat_count_down']){
	                    
        	            //判断用户是否有足够的保证金
        	            $sql     = "SELECT `coins_money` FROM {$this->_prefix['primary']}finance_balance WHERE `uid` = ".$this->_user['uid'];
        	            $coins_money = $this->db->getone($sql);
        	           
    	               if($coins_money-$goodsDetail['pat_guarantee_price']>=0){
    	                   //查出商品是否拍卖结束
    	                   $sql     = "SELECT `pat_status` FROM {$this->_prefix['primary']}goods_pat WHERE `goods_id` = ".$goods_id ." AND `start_time`=".$goodsDetail['pat_start_time']." AND `end_time`=".$goodsDetail['pat_count_down'];
    	                   $pat_goods = $this->db->getrow($sql);
    	                   $stat= true;//全局变量
    	                   
    	                     if($pat_goods['pat_status']>0){
    	                         $stat = false;
    	                         $arr['code'] = 905;
    	                         $arr['msg'] = "拍卖商品已结束";
    	                     }
    	                     $this->db->begin();
    	                     $rollback = false;
    	                    
    	                     //判断是否创建拍卖主表
    	                     if($stat){
    	                       
    	                         if(empty($pat_goods)){
        	                         //插入拍卖主表关联信息
    	                             $sql = "INSERT INTO {$this->_prefix['primary']}goods_pat
                                    (`goods_id`,`create_time`,`start_time`,`end_time`,`pat_guarantee_price`) VALUES
    	                            (" . $goods_id . "," . $_SERVER['REQUEST_TIME'] . ",".$goodsDetail['pat_start_time'].",".$goodsDetail['pat_count_down'].",'".$goodsDetail['pat_guarantee_price']."')";
                                    $rd = $this->sql($sql);
                                  
    	                         }else{
    	                             
    	                             $rd  =true;
    	                         }
                                if($rd){
                                    
                                    //$total_bid_price = $goodsDetail['pat_guarantee_price']+$goodsDetail['pat_fixed_price'];
                                    //查出叫价最新价格
                                    $sql     = "SELECT `bid_price`,`uid` FROM {$this->_prefix['primary']}order_bidrecord WHERE `goods_id` = ".$goods_id ." AND `create_time`>=".$goodsDetail['pat_start_time']." AND `create_time`<".$goodsDetail['pat_count_down']." ORDER BY `create_time` DESC";
                                    $order_bid = $this->db->getrow($sql);
                                    $news_money = $order_bid['bid_price'];
                                    $cions_type = 0;
                                    
                                    //出价金额计算
                                    if($news_money>0){
                                    //判断是否超出封顶价
                                        if(($news_money+$goodsDetail['pat_fixed_price'])>$goodsDetail['pat_max_price']){
                                            $stat = false;
                                            $arr['code'] = 907;
                                            $arr['msg'] = "商品已被拍中";
                                        }else{
                                            if($this->_user['uid'] != intval($order_bid['uid'])){
                                                $cions_type = 2;
                                                
                                            }else{
                                                
                                                $total_bid_price = $goodsDetail['pat_fixed_price']; 
                                            }
                                            $bid_price = number_format($news_money+$goodsDetail['pat_fixed_price'], 2, '.', '');
                                        }
                                    }else{
                                            $cions_type = 1;
                                            $bid_price = number_format($goodsDetail['pat_min_price']+$goodsDetail['pat_fixed_price'], 2, '.', '');
                                        
                                    }
                                 /*   if($coins_money>=$total_bid_price){ */
                                      //九云币消费变动\
                                      
                                       if($stat && $cions_type>0){
                                            $stat = false;
                                            
                                            
                                            $sql = "UPDATE {$this->_prefix['primary']}finance_balance SET `coins_money`=`coins_money`-" . $goodsDetail['pat_guarantee_price'] . ",`freeze_coins_money`=`freeze_coins_money`+" . $goodsDetail['pat_guarantee_price'] . ",`update_time`=" . $_SERVER['REQUEST_TIME'] . " WHERE `uid`=" . $this->_user['uid'];
                                            $rd  = $this->sql($sql);
                                           
                                            if ($rd > 0) {
                                                
                                                //
                                                $balance = $this->db->getrow("SELECT `coins_money`,`freeze_coins_money`,`freeze`,`money` FROM {$this->_prefix['primary']}finance_balance WHERE `uid`=" . $this->_user['uid']);
                                                //插入资金明细
                                                $detail_note = '拍一元拍商品成功，冻结九云币' . $goodsDetail['pat_guarantee_price'] . '元';
                                                $sql         = "INSERT INTO {$this->_prefix['primary']}finance_coins (`uid`,`event`,`cmoney`,`money`,`freeze`,`create_time`,`note`) VALUES (" . $this->_user['uid'] . ",4," . $goodsDetail['pat_guarantee_price'] . "," . $balance['coins_money'] . "," . $balance['freeze_coins_money']. "," . $_SERVER['REQUEST_TIME'] . ",'" . $detail_note . "')";
                                                $detail_id   = $this->sql($sql);
                                               
                                            }else{
                                                $arr['code'] = 917;
                                                
                                            }
                                            
                                            if($detail_id){
                                                
                                                //查出叫价是否是机器人
                                                $sql     = "SELECT `member_type` FROM {$this->_prefix['primary']}member_account WHERE  `uid` = ".$order_bid['uid'] ;
                                                $member_type = $this->db->getone($sql);
                                                if($cions_type==2 && $member_type){
                                                    //退还上一个用户九云币
                                                    $sql = "UPDATE {$this->_prefix['primary']}finance_balance SET `coins_money`=`coins_money`+" . $goodsDetail['pat_guarantee_price'] . ",`freeze_coins_money`=`freeze_coins_money`-" . $goodsDetail['pat_guarantee_price'] . ",`update_time`=" . $_SERVER['REQUEST_TIME'] . " WHERE `uid`=" . $order_bid['uid'];
                                                    $rd  = $this->sql($sql);
                                                     
                                                    if ($rd > 0) {
                                                    
                                                        //
                                                        $balance = $this->db->getrow("SELECT `coins_money`,`freeze_coins_money`,`freeze`,`money` FROM {$this->_prefix['primary']}finance_balance WHERE `uid`=" . $order_bid['uid']);
                                                        //插入资金明细
                                                        $detail_note = '手机号码为'.$this->_user['mobile'].'拍一元拍商品成功，解冻九云币' . $goodsDetail['pat_guarantee_price'] . '元';
                                                        $sql         = "INSERT INTO {$this->_prefix['primary']}finance_coins (`uid`,`event`,`cmoney`,`money`,`freeze`,`create_time`,`note`) VALUES (" . $order_bid['uid'] . ",5," . $goodsDetail['pat_guarantee_price'] . "," . $balance['coins_money'] . "," . $balance['freeze_coins_money']. "," . $_SERVER['REQUEST_TIME'] . ",'" . $detail_note . "')";
                                                        $detail_id   = $this->sql($sql);
                                                         if($detail_id){
                                                             $stat = true;
                                                             
                                                         }else{
                                                             
                                                             $arr['code'] = 920;
                                                         }
                                                    }else{
                                                        
                                                        $arr['code'] = 919;
                                                    }
                                                    
                                                }else{
                                                    
                                                    $stat = true;
                                                }
                                                
                                                
                                            }else{
                                                
                                                $arr['code'] = 918;
                                            }
                                            
                                            
                                        }else{
                                            $arr['code'] = 921;
                                            
                                        }
                                        
                                        if($stat){
                                            
                                            $stat = false;
                                            //修改用户挥拍次数
                                            $sql = "UPDATE {$this->_prefix['primary']}member_account SET `pat_num`=`pat_num`+1,`update_time`=".time()."  WHERE `uid`=" .$this->_user['uid'];
                                            $mb  = $this->sql($sql);
                                            
                                            if($mb){
                                                $stat = true;
                                                
                                            }else{
                                                $arr['code'] = 923;
                                                
                                            }
                                            
                                        }
                                        
                                        if($stat){
                                                //插入自己/上一级 、上二级，补偿金额
                                                $mu = $this->getMemberInfo();//自己
                                                if($mu['pid']>0){
                                                    
                                                    $pu = $this->getMemberInfo($mu['pid']);//上一级
                                                     if($pu['pid']>0){
                                                         $ppu = $this->getMemberInfo($pu['pid']);//上二级
                                                     }
                                                }
                                                //print_r($pu);die;
                                                $equalize_price = $one_equalize_price=$two_equalize_price=0;
                                                //获取返利配置
                                                $con = $this->getSetting();
                                                //补偿金
                                                //自己
                                                $equalize_price = number_format(($con['my_pat'] *0.01) * $goodsDetail['pat_fixed_price'], 2, '.', '');
                                                //插入叫价和补偿记录
                                                $sql = "INSERT INTO {$this->_prefix['primary']}order_bidrecord
                                                (`goods_id`,`create_time`,`uid`,`bid_price`,`equalize_price`) VALUES
                                                (" . $goods_id . "," . $_SERVER['REQUEST_TIME'] . ",".$this->_user['uid'].",'".$bid_price."','".$equalize_price."')";
                                                $ob_id = $this->sql($sql);
                                                /* if($ob_id){
                                                     
                                                     $stat = false;
                                                     //九云币变动
                                                      $sql = "UPDATE {$this->_prefix['primary']}finance_balance SET `coins_money`=`coins_money`-" . $goodsDetail['pat_fixed_price'] . ",`update_time`=" . $_SERVER['REQUEST_TIME'] . " WHERE `uid`=" . $this->_user['uid'];
                                                     $rd  = $this->sql($sql);
                                                      
                                                     if ($rd > 0) {
                                                     
                                                         //
                                                         $balance = $this->db->getrow("SELECT `coins_money`,`freeze_coins_money`,`freeze`,`money` FROM {$this->_prefix['primary']}finance_balance WHERE `uid`=" . $this->_user['uid']);
                                                         //插入资金明细
                                                         $detail_note = '拍一元拍商品成功，消费九云币' . $goodsDetail['pat_fixed_price'] . '元';
                                                         $sql         = "INSERT INTO {$this->_prefix['primary']}finance_coins (`uid`,`event`,`cmoney`,`money`,`freeze`,`create_time`,`note`) VALUES (" . $this->_user['uid'] . ",2," . $goodsDetail['pat_fixed_price'] . "," . $balance['coins_money'] . "," . $balance['freeze_coins_money']. "," . $_SERVER['REQUEST_TIME'] . ",'" . $detail_note . "')";
                                                         $detail_id   = $this->sql($sql);
                                                         if($detail_id){
                                                             
                                                             $stat = true;
                                                         }else{
                                                             
                                                             $arr['code'] = 915;
                                                         }
                                                          
                                                   }else{
                                                       $arr['code'] = 914;
                                                       
                                                   } 
                                                      
                                                     
                                                     
                                                 }*/
                                                if($ob_id){
                                       
                                                    //增加资金
                                                    $sql = "UPDATE {$this->_prefix['primary']}finance_balance SET `money`=`money`+" . $equalize_price . ",`update_time`=" . $_SERVER['REQUEST_TIME'] . " WHERE `uid`=" . $this->_user['uid'];
                                                    $rd  = $this->sql($sql);
                                                    if ($rd > 0) {
                                                        //插入资金明细
                                                        $detail_note = '自己拍一元拍商品成功，返利补偿金' . $equalize_price . '元';
                                                        $sql         = "INSERT INTO {$this->_prefix['primary']}finance_detail (`uid`,`event`,`cmoney`,`money`,`freeze`,`dataid`,`table_name`,`create_time`,`note`) VALUES (" . $this->_user['uid'] . ",2," . $equalize_price . "," . ($balance['money']+$equalize_price) . "," . $balance['freeze']. "," . $ob_id . ",'order_bidrecord'," . $_SERVER['REQUEST_TIME'] . ",'" . $detail_note . "')";
                                                        $detail_id   = $this->sql($sql);
                                                       
                                                        if($detail_id){
                                                            
                                                             //上一级
                                                            if($pu['uid']){
                                                                
                                                                $balance = $this->db->getrow("SELECT `money`,`freeze` FROM {$this->_prefix['primary']}finance_balance WHERE `uid`=" . $pu['uid']);
                                                                $one_equalize_price = number_format(($con['one_pat'] *0.01) * $goodsDetail['pat_fixed_price'], 2, '.', '');
                                                                //增加资金
                                                                $sql = "UPDATE {$this->_prefix['primary']}finance_balance SET `money`=`money`+" . $one_equalize_price . ",`update_time`=" . $_SERVER['REQUEST_TIME'] . " WHERE `uid`=" . $pu['uid'];
                                                                $rd  = $this->sql($sql);
                                                                if ($rd > 0) {
                                                                    //插入资金明细
                                                                    $detail_note = $mu['name'].'拍一元拍商品成功，返利补偿金' . $one_equalize_price . '元';
                                                                    $sql         = "INSERT INTO {$this->_prefix['primary']}finance_detail (`uid`,`event`,`cmoney`,`money`,`freeze`,`dataid`,`table_name`,`create_time`,`note`) VALUES (" . $pu['uid'] . ",2," . $one_equalize_price . "," . ($balance['money']+$one_equalize_price) . "," . $balance['freeze']. "," . $ob_id . ",'order_bidrecord'," . $_SERVER['REQUEST_TIME'] . ",'" . $detail_note . "')";
                                                                    $detail_id   = $this->sql($sql);
                                                                    if($detail_id){
                                                                        //上二级
                                                                        if($ppu['uid']){
                                                                            $balance = $this->db->getrow("SELECT `money`,`freeze` FROM {$this->_prefix['primary']}finance_balance WHERE `uid`=" . $ppu['uid']);
                                                                            $two_equalize_price = number_format(($con['two_pat'] *0.01) * $goodsDetail['pat_fixed_price'], 2, '.', '');
                                                                            //增加资金
                                                                            $sql = "UPDATE {$this->_prefix['primary']}finance_balance SET `money`=`money`+" . $two_equalize_price . ",`update_time`=" . $_SERVER['REQUEST_TIME'] . " WHERE `uid`=" . $ppu['uid'];
                                                                            $rd  = $this->sql($sql);
                                                                            if ($rd > 0) {
                                                                                //插入资金明细
                                                                                $detail_note = $mu['name'].'拍一元拍商品成功，返利补偿金' . $two_equalize_price . '元';
                                                                                $sql         = "INSERT INTO {$this->_prefix['primary']}finance_detail (`uid`,`event`,`cmoney`,`money`,`freeze`,`dataid`,`table_name`,`create_time`,`note`) VALUES (" . $ppu['uid'] . ",2," . $two_equalize_price . "," . ($balance['money']+$two_equalize_price) . "," . $balance['freeze']. "," . $ob_id . ",'order_bidrecord'," . $_SERVER['REQUEST_TIME'] . ",'" . $detail_note . "')";
                                                                                $detail_id   = $this->sql($sql);
                                                                                if($detail_id){
                                                                                    
                                                                                    $stat = true;
                                                                                }else{
                                                                                    $arr['code'] = 913;
                                                                                    
                                                                                }
                                                                            }else{
                                                                                $arr['code'] = 912;
                                                                                
                                                                            }
                                                                        }else{
                                                                            $stat = true;
                                                                            
                                                                        }
                                                                        
                                                                    }else{
                                                                        $arr['code'] = 911;
                                                                        
                                                                    }
                                                                }else{
                                                                    $arr['code'] = 910;
                                                                    
                                                                }
                                                           
                                                            }else{
                                                                $stat = true;
                                                                
                                                            }
                                                            
                                                            //云米返利
                                                            if($stat){
                                                               
                                                               $rollback =  $this->getPidLevel(0,$con,$goodsDetail['pat_fixed_price'],0,$goods_id);
                                                               
                                                            }
                                                            
                                                        }else{
                                                            $arr['code'] = 909;
                                                            
                                                        }
                                                    }else{
                                                        $arr['code'] = 908;
                                                        
                                                    }
                                                    
                                                }else{
                                                    
                                                    $arr['code'] = 907;
                                                }
                                                
                                           
                                          
                                        
                                           }
                                        
                                  /*    }else{
                                         $arr['code'] = 916;
                                         $arr['msg'] = "你的九云币不足";
                                     } */
                                }else{
                                    $arr['code'] = 906;
                                    
                                }
    	                         
    	                     } 
    	                   
    	                   
    	               }else{
    	                   $arr['code'] = 904;
    	                   $arr['msg'] = "你的九云币不足";
    	               }
    	            }else{
    	                $arr['code'] = 903;
    	                $arr['msg'] = "拍卖还没有开始";
    	            }
	            
	            }else{
	                $arr['code'] = 902;
	               
	                
	            }
	            
	        }else{
	            $arr['code'] = 901;
	            
	        }
	        if($rollback){
	            $this->db->commit();
	            $arr['code'] = 200;
	            $arr['msg'] = "购买成功！";
	            $arr['price'] = $bid_price;
	            
	        }else{
	            $this->db->rollback();
	           
	            
	        }
	        
	        return $arr;
	        
	    } 
	    
	    //定时没秒钟修改拍卖状态 
	    
	    public function savePatStatus()
	    {
	        
	        $time = time();
	        //查出所有开始的一元拍商品
	         $data  = $this->getPatGoodsList($time);
	         //print_r($data);die;
	         $done = false;
	         if($data){
	            
    	        //根据封顶价和倒计时进行一元拍商品更新
    	        foreach($data as $v){
    	            $stat = false;
    	            $pay_down_time = 0;
    	            
    	            $sql     = "SELECT * FROM {$this->_prefix['primary']}goods_pat WHERE `goods_id` = ".$v['goods_id'] ." AND `create_time`>=".$v['pat_start_time']." AND `create_time`<".$v['pat_count_down'];
    	            $pat = $this->db->getrow($sql);
    	         
    	           
    	            $pay_down_time = $time+($v['pat_order_count_down']*60);//付款倒计时
    	           
    	            //$ctime[$v['goods_id']] >=$v['pat_start_time'] && $ctime[$v['goods_id']] < $v['pat_count_down']
    	           // echo $pat_order_count_down;die;
    	           if(empty($pat) || $pat['pat_status']==0){
    	            if(!empty($pat)){
    	                 //查出叫价最新价格
                        $sql     = "SELECT `bid_price`,`id` FROM {$this->_prefix['primary']}order_bidrecord WHERE `goods_id` = ".$v['goods_id'] ." AND `create_time`>=".$pat['start_time']." AND `create_time`<".$pat['end_time']." ORDER BY `bid_price` DESC";
                        $order_pat = $this->db->getrow($sql);
                        
    	            }else{
    	                
    	                $order_pat['bid_price'] = 0;
    	            }
                  
                        //出价金额计算
                       
                            //判断是否超出封顶价或者一元拍商品是否已结束
                            if($order_pat['bid_price']>=$v['pat_max_price'] || $time>=$v['pat_count_down']){
        	                  
        	                   //没有的话增加主表记录/有的话修改主表状态
        	                      if($order_pat['bid_price']<=0){
        	                       //插入拍卖主表关联信息
        	                       $sql = "INSERT INTO {$this->_prefix['primary']}goods_pat
        	                       (`goods_id`,`create_time`,`pat_status`,`start_time`,`end_time`) VALUES
        	                       (" . $v['goods_id'] . "," . $time . ",2,".$v['pat_start_time'].",".$v['pat_count_down'].")";
        	                       $rd = $this->sql($sql);
        	                       if($rd){
        	                           
        	                           $stat  =true;
        	                       }
        	                      
        	                   }else{
        	                       
        	                    
        	                       //修改付款倒计时
        	                       $sql = "UPDATE {$this->_prefix['primary']}goods_pat SET `pat_status`= 1,`oid`=".$order_pat['id']." WHERE `goods_id`=" . $v['goods_id']. " AND `start_time`=".$v['pat_start_time']." AND `end_time`=".$v['pat_count_down'];
        	                       $rs  = $this->sql($sql);
        	                       if($rs){
        	                          
            	                      
            	                           $sql = "UPDATE {$this->_prefix['primary']}order_bidrecord SET `status`= 1,`pay_down_time`=".$pay_down_time." WHERE `id`=" .$order_pat['id'];
            	                           $op = $this->sql($sql);
            	                         if($op){
            	                             
            	                              $stat  =true;
            	                         }
        	                           
        	                       }
        	                       
        	                       
        	                   }
        	                   
        	                   if($stat){
        	                       
        	                       if($v['pat_repeat_num']>0){
        	                           $times  = $time+10;
        	                           $pat_count_down =  $times+($v['pat_count_down']-$v['pat_start_time']);
            	                       $sql = "UPDATE {$this->_prefix['primary']}goods_data SET `pat_start_time`= ".$times.",`pat_count_down`=".$pat_count_down.",`pat_repeat_num`=`pat_repeat_num`-1 WHERE `goods_id`=" . $v['goods_id'];
            	                       $grs  = $this->sql($sql);
            	                     
            	                       if($grs){
            	                           $code = 200;
            	                           
            	                       }
        	                      
        	                       }else{
        	                         
        	                         $code = 200;
        	                      }
        	                   }
                           
                           }else{
                               $code = 903;
                               
                           }
    	             }else{
    	                 
    	                 $code = 902;
    	                 
    	                 
    	             }
    	            
    	        }
    	        
    	    
    	        
	        }else{
	            
	            $code = 901;
	        }
	        
	        //付款倒计时
	        $sql = "SELECT o.*,g.`pat_guarantee_price` FROM {$this->_prefix['primary']}order_bidrecord o LEFT JOIN {$this->_prefix['primary']}goods_pat g ON g.`oid`= o.`id` WHERE o.`status`=1 AND o.`pat_down_status`=0";
	        $pat_order  = $this->sql($sql);
	        if($pat_order){
    	        foreach($pat_order as $v){
    	            //如果付款倒计时结束
    	            if(time()>=$v['pay_down_time']){
    	                //如果有未付款订单
    	               if($v['pay_status']==0){
    	                   
    	                   if($v['order_id']>0){
        	                   $sql = "DELETE FROM {$this->_prefix['primary']}order_info WHERE `order_id`=" .$v['order_id'];
        	                   $rds = $this->sql($sql);
        	                   if($rds){
        	                       $sql = "DELETE FROM {$this->_prefix['primary']}order_goods WHERE `order_id`=" .$v['order_id'];
        	                       $og = $this->sql($sql);
        	                       
        	                      
        	                   }
    	                   
    	                   }else{
    	                       $og = true;
    	                       
    	                   }
    	                   
    	                  if($og){
                               $sql = "UPDATE {$this->_prefix['primary']}order_bidrecord SET `order_id`=0,`pat_down_status`=1 WHERE `id`=" . $v['id'];
                               $rd  = $this->sql($sql);
        	                           
        	                   }
        	                  
    	                   $member = $this->getMemberInfo($v['uid']);
    	                  if($rd){
        	                   if($member['member_type']){
        	                   //修改拍卖状态保证金回滚
        	                   $sql = "UPDATE {$this->_prefix['primary']}finance_balance SET `freeze_coins_money`=`freeze_coins_money`-" . $v['pat_guarantee_price'] . ",`update_time`=" . $_SERVER['REQUEST_TIME'] . " WHERE `uid`=" . $v['uid'];
        	                   $rd  = $this->sql($sql);
        	                   if ($rd > 0) {
        	                   
        	                       //
        	                       $balance = $this->db->getrow("SELECT `coins_money`,`freeze_coins_money` FROM {$this->_prefix['primary']}finance_balance WHERE `uid`=" . $v['uid']);
        	                       //插入资金明细
        	                       $detail_note = '一元拍失约扣除九云币' . $v['pat_guarantee_price'] . '元';
        	                       $sql         = "INSERT INTO {$this->_prefix['primary']}finance_coins (`uid`,`event`,`cmoney`,`money`,`freeze`,`dataid`,`table_name`,`create_time`,`note`) VALUES (" . $v['uid'] . ",15," . $v['pat_guarantee_price'] . "," . $balance['coins_money'] . "," . $balance['freeze_coins_money']. "," . $v['id'] . ",'order_bidrecord'," . $_SERVER['REQUEST_TIME'] . ",'" . $detail_note . "')";
        	                       $detail_id   = $this->sql($sql);
        	                       if($detail_id){
        	                           
        	                           $code = 200;
        	                       }
        	                        
        	                   }
        	                 }else{
        	                     $code = 200;
        	                     
        	                 }
    	                  }
    	               }
    	        
    	            }
    	            
    	        }
    	        
	        }
	        
	        return  $code;
	   }
	    
	   
	   //机器人跟拍
	   public function saveFollowing()
	   {
	       $time = time();
	       $sql  = "SELECT `goods_id`,`pat_start_time`,`risk_floor_price`,`is_pat`,`pat_max_price`,`pat_min_price`,`pat_fixed_price`,`pat_guarantee_price`,`pat_count_down`,`pat_period_day`,`pat_start_time`,`is_risk`,`risk_frequency`,`risk_frequency_time` FROM {$this->_prefix['primary']}goods_data  WHERE `is_delete`=0  AND `pat_start_time` <=".$time." AND `pat_count_down`>".$time." AND `is_pat`=1 AND `is_risk`=1";
	       $data = $this->sql($sql);
	       //获取返利配置
	       $con = $this->getSetting();
	       //补偿金
	       //自己
	     
	       if($data){
    	       foreach($data as $v){
    	         /*   $sql     = "SELECT * FROM {$this->_prefix['primary']}goods_pat WHERE `goods_id` = ".$v['goods_id'] ." AND `create_time`>=".$v['pat_start_time']." AND `create_time`<".$v['pat_count_down'];
    	           $pat = $this->db->getrow($sql); */
    	           
    	           //if(empty($pat) || $pat['pat_status']==0){//如果拍卖没结束
    	          
    	           //查出叫价最新价格
                    $sql     = "SELECT `bid_price`,`id`,`uid` FROM {$this->_prefix['primary']}order_bidrecord WHERE `goods_id` = ".$v['goods_id'] ." AND `create_time`>=".$v['pat_start_time']." AND `create_time`<".$v['pat_count_down']." ORDER BY `bid_price` DESC";
                    $order_pat = $this->db->getrow($sql);
                  
                   
                    if(intval($order_pat['bid_price'])<$v['risk_floor_price'] && $time > $v['risk_frequency_time']){//如果最新叫价小于保底价
                        //随机UID
                        
                        $rand_arr = $this->getMemberType();
                	    $temp = array_rand($rand_arr,1);  //随机取1个
                	    $uid = $rand_arr[$temp]['uid'];
                	  
                        if(empty($order_pat)){
                            $order_pat['bid_price'] = $v['pat_min_price'];
                            //插入拍卖主表关联信息
                            $sql = "INSERT INTO {$this->_prefix['primary']}goods_pat
                            (`goods_id`,`create_time`,`start_time`,`end_time`) VALUES
                            (" . $v['goods_id'] . "," . $time . ",".$v['pat_start_time'].",".$v['pat_count_down'].")";
                            $rd = $this->sql($sql);
                        }
                        //跳价
                        $bid_price = number_format($order_pat['bid_price']+$v['pat_fixed_price'], 2, '.', '');
                          
                        //自己补偿金
                        $equalize_price = number_format(($con['my_pat'] *0.01) * $v['pat_fixed_price'], 2, '.', '');
                        //插入跳价记录
                        $sql = "INSERT INTO {$this->_prefix['primary']}order_bidrecord
                        (`goods_id`,`create_time`,`uid`,`bid_price`,`equalize_price`) VALUES
                        (" . $v['goods_id'] . "," . $time . ",".$uid.",'".$bid_price."','".$equalize_price."')";
                        $ob_id = $this->sql($sql);
                        //更新风控频率
                        if($ob_id){
                           
                            $risk_frequency  = $time+$v['risk_frequency'];
                          
                            $sql = "UPDATE {$this->_prefix['primary']}goods_data SET `risk_frequency_time`=" . $risk_frequency . ",`last_update`=" . $time . " WHERE `goods_id`=" . $v['goods_id'];
                            $rb  = $this->sql($sql);
                            if($rb){
                                //查出叫价是否是机器人
                                $sql     = "SELECT `member_type` FROM {$this->_prefix['primary']}member_account WHERE  `uid` = ".$order_pat['uid'] ;
                                $member_type = $this->db->getone($sql);
                                if($member_type && $order_pat){
                                    //退还上一个用户九云币
                                    $sql = "UPDATE {$this->_prefix['primary']}finance_balance SET `coins_money`=`coins_money`+" . $v['pat_guarantee_price'] . ",`freeze_coins_money`=`freeze_coins_money`-" . $v['pat_guarantee_price'] . ",`update_time`=" . $_SERVER['REQUEST_TIME'] . " WHERE `uid`=" . $order_pat['uid'];
                                    $rd  = $this->sql($sql);
                                     
                                    if ($rd > 0) {
                                
                                        //
                                        $balance = $this->db->getrow("SELECT `coins_money`,`freeze_coins_money`,`freeze`,`money` FROM {$this->_prefix['primary']}finance_balance WHERE `uid`=" . $order_pat['uid']);
                                        //插入资金明细
                                        $detail_note = '一元拍出局，解冻九云币' . $v['pat_guarantee_price'] . '元';
                                        $sql         = "INSERT INTO {$this->_prefix['primary']}finance_coins (`uid`,`event`,`cmoney`,`money`,`freeze`,`create_time`,`note`) VALUES (" . $order_pat['uid'] . ",5," . $v['pat_guarantee_price'] . "," . $balance['coins_money'] . "," . $balance['freeze_coins_money']. "," . $_SERVER['REQUEST_TIME'] . ",'" . $detail_note . "')";
                                        $detail_id   = $this->sql($sql);
                                        if($detail_id){
                                            
                                            $code = 200;
                                        }
                                    }
                                
                                }else{
                                    
                                    $code = 200;
                                }
                                
                            }
                        }
                      
                    }else{
                        
                        $code  = 902;
                    }
    	           //}
    	       }
	       
	       }else{
	           $code  = 903;
	           
	       }
	       return $code;
	   }
	    // 
	    public function getPidLevel($uid=0,$con,$bid_price,$level_rate=0,$goods_id)
	    {
	        $stat = true;
	      
	        //$level_rate 本级比列   $bid_price 跳价  $con全局配置信息
	            if(empty($uid)){
	        
	                $where = " AND `uid`=".$this->_user['uid'];
	            }else{
	        
	                $where = " AND `uid` =".$uid;
	            }
	        
	           
	        
	        
	            $sql="SELECT `uid`,`pid`,`member_level` FROM {$this->_prefix['primary']}member_account WHERE  `stat`=1 ".$where;
	            $members = $this->db->getrow($sql);
	           //
	          
	            if(!empty($members)){
	                
	                if($members['member_level']==1){
	                    
	                    if(($con['freeze_vip_pat']-$level_rate)>0){
	                        $cloud_price = number_format($bid_price * ($con['freeze_vip_pat']-$level_rate)*0.01, 2, '.', '');
	                        
	                    }else{
	                        $cloud_price = number_format($bid_price * $con['freeze_vip_pat'] * 0.01, 2, '.', '');
	                        
	                    }
	                    $level_rate = $con['freeze_vip_pat'];
	                    
	                   
	                    
	                }elseif($members['member_level']==2){
	                    
	                    if(($con['freeze_shop_pat']-$level_rate)>0){
	                        $cloud_price = number_format($bid_price * ($con['freeze_shop_pat']-$level_rate)*0.01, 2, '.', '');
	                         
	                    }else{
	                        $cloud_price = number_format($bid_price * $con['freeze_shop_pat'] * 0.01, 2, '.', '');
	                         
	                    }
	                    $level_rate = $con['freeze_shop_pat'];
	                    
	                }elseif($members['member_level']==3){
	                    
	                    if(($con['freeze_wareh_pat']-$level_rate)>0){
	                        $cloud_price = number_format($bid_price * ($con['freeze_wareh_pat']-$level_rate)*0.01, 2, '.', '');
	                    
	                    }else{
	                        $cloud_price = number_format($bid_price * $con['freeze_wareh_pat'] * 0.01, 2, '.', '');
	                    
	                    }
	                    $level_rate = $con['freeze_wareh_pat'];
	                    
	                }elseif($members['member_level']==4){
	                    if(($con['freeze_county_pat']-$level_rate)>0){
	                        $cloud_price = number_format($bid_price * ($con['freeze_county_pat']-$level_rate)*0.01, 2, '.', '');
	                         
	                    }else{
	                        $cloud_price = number_format($bid_price * $con['freeze_county_pat'] * 0.01, 2, '.', '');
	                         
	                    }
	                    $level_rate = $con['freeze_county_pat'];
	                    
	                }elseif($members['member_level']==5){
	                    
	                    if(($con['freeze_city_pat']-$level_rate)>0){
	                        $cloud_price = number_format($bid_price * ($con['freeze_city_pat']-$level_rate)*0.01, 2, '.', '');
	                    
	                    }else{
	                        $cloud_price = number_format($bid_price * $con['freeze_city_pat'] * 0.01, 2, '.', '');
	                    
	                    }
	                    $level_rate = $con['freeze_city_pat'];
	                    
	                }elseif($members['member_level']==6){
	                    if(($con['freeze_province_pat']-$level_rate)>0){
	                        $cloud_price = number_format($bid_price * ($con['freeze_province_pat']-$level_rate)*0.01, 2, '.', '');
	                         
	                    }else{
	                        $cloud_price = number_format($bid_price * $con['freeze_province_pat'] * 0.01, 2, '.', '');
	                         
	                    }
	                    
	                    $level_rate = $con['freeze_province_pat'];
	                }else{
	                    $cloud_price = 0;
	                    $level_rate = 0;
	                }
	            
	                //添加云米记录
	                if($cloud_price>0){
	                   
	                    $sql = "UPDATE {$this->_prefix['primary']}finance_balance SET `cloud_money`=`cloud_money`+" . $cloud_price . ",`update_time`=" . $_SERVER['REQUEST_TIME'] . " WHERE `uid`=" . $members['uid'];
	                    $rd  = $this->sql($sql);
	                 
	                    if ($rd > 0) {
	                        //插入云米明细
	                        $sql         = "INSERT INTO {$this->_prefix['primary']}order_cloud_detail (`goods_id`,`member_level`,`uid`,`cloud_price`,`create_time`) VALUES (" . $goods_id . ",".$members['member_level']."," . $members['uid'] . ",'" . $cloud_price. "'," . $_SERVER['REQUEST_TIME'] . ")";
	                        $detail_id   = $this->sql($sql);
	                        if(!$detail_id){
	                            $stat = false;
	                          
	                        }
	                            
	                            
	                    }else{
	                        $stat = false;
	                        
	                    }
	                }
	                
	                    if($members['pid']>0 && $stat){
	                        
	                        return $this->getPidLevel($members['pid'],$con,$bid_price,$level_rate,$goods_id);
	                    }
	                
	                
	               
	              
	            }
	        
	          
	            return $stat;
	        
	        
	       
	        
	    }
	    // 获取设置
	    public function getSetting()
	    {
	        $sql     = "SELECT `setting` FROM {$this->_prefix['primary']}setting ORDER BY `setting_id` DESC LIMIT 1";
	        $setting = $this->db->getone($sql);
	        if ($setting != "") {
	            return json_decode($setting, true);
	        }
	    
	    }
	    //用户信息
	    public function getMemberInfo($uid=0)
	    {
	       
	        if(!$uid){
	           $uid =  $this->_user['uid'];
	            
	        }
	        $where = "`stat` =1 AND `uid`=".$uid;
	        $sql     = "SELECT * FROM {$this->_prefix['primary']}member_account WHERE ".$where ;
	        $data = $this->db->getrow($sql);
	        return $data;
	    }
	    
	    //获取假用户信息
	    public function getMemberType()
	    {
	    
	        
	      
	        $sql     = "SELECT `uid` FROM {$this->_prefix['primary']}member_account WHERE   `member_type`=0" ;
	        $data = $this->sql($sql);
	        return $data;
	    }
	    //用户资金信息
	    public function getMemberMoney($uid=0)
	    {
	    
	        if(!$uid){
	            $uid =  $this->_user['uid'];
	             
	        }
	        $where = "`uid`=".$uid;
	        $sql     = "SELECT * FROM {$this->_prefix['primary']}finance_balance WHERE ".$where;
	        $data = $this->db->getrow($sql);
	        
	        return $data;
	    }
	    //获取自己的排拍中或者正在拍数据
	    public function getPatBeging()
	    {
	       
	         $sql = "SELECT * FROM (SELECT * FROM {$this->_prefix['primary']}order_bidrecord ORDER BY `bid_price` DESC)b  WHERE b.`status`=0 AND b.`uid`=".$this->_user['uid']." GROUP BY b.`goods_id` ";
             $data  = $this->sql($sql);
             $num = 0;
	        if($data){
	            foreach($data as $k=>$v){
	                $sql     = "SELECT * FROM {$this->_prefix['primary']}goods_pat WHERE `goods_id`=".$v['goods_id']." AND `start_time`<=".$v['create_time']." AND `end_time`>".$v['create_time'] ." AND `pat_status`=0";
	                $pat = $this->db->getrow($sql);
	              
	                if(time()<$pat['end_time']){
	                    
	                    $sql     = "SELECT `goods_name` FROM {$this->_prefix['primary']}goods_data WHERE `goods_id`=".$v['goods_id'];
	                    $goods_name = $this->db->getone($sql);
	                    $v['pat_count_down'] = $pat['end_time'];
	                    $v['goods_name'] = $goods_name;
	                    $pat_being[] = $v;
	                    $num +=1;
	                }
	               
	            }
	        }
	        
	       
	        $arr['pat_being'] = $pat_being;
	        $arr['num'] = $num;

	        return $arr;
	    
	    }
	    
	    
	    //获取自己的排拍中或者正在拍数据
	    public function getPatList()
	    {
	    
	         $sql = "SELECT o.`bid_price`,o.`id`,o.`goods_id`,o.`pay_down_time`,o.`status`,g.`goods_name` FROM {$this->_prefix['primary']}order_bidrecord o LEFT JOIN {$this->_prefix['primary']}goods_data g ON g.`goods_id`= o.`goods_id` WHERE  o.`status`=1 AND o.`pay_status`=0 AND o.`uid`=".$this->_user['uid']." ORDER BY o.`create_time` DESC";
             $data = $this->sql($sql);
	         
	        if($data){
	            foreach($data as $k=>$v){
	             
	                    if(time()<$v['pay_down_time']){
	                      
	                         $pat_success[] = $v;
	                      
	                    }else{
	                        $pat_past_time[] = $v;
	                        
	                    }
	                   
	                    
	                    
	              
	    
	            }
	        }
	         
	        $arr['pat_success'] = $pat_success;
	      
	        $arr['pat_past_time'] = $pat_past_time;
	    
	        return $arr;
	         
	    }
	    // 获取区域信息
	    public function getRegion()
	    {
	        $sql = "SELECT `region_id`,`region_name`,`parent_id` FROM {$this->_prefix['primary']}region ORDER BY `parent_id` ASC,`region_id` ASC";
	    
	        $data = $this->sql($sql);
	        if ($data) {
	            foreach ($data as $r) {
	                $new[$r['parent_id']][$r['region_id']] = $r;
	            }
	            return $new;
	        }
	    
	    }
	    //获取拍卖单个信息
	    public function getPatDetail($id)
	    {
	        $sql = "SELECT o.`bid_price`,o.`id`,o.`pay_down_time`,o.`status`,o.`order_id`,g.* FROM {$this->_prefix['primary']}order_bidrecord o LEFT JOIN {$this->_prefix['primary']}goods_data g ON g.`goods_id`= o.`goods_id` WHERE  o.id=".$id;
	        $info = $this->db->getrow($sql);
	        if($info){
	            
	            if ($info['thumb_docid'] > 0) {
	            
	                $pic             = $this->getPic($info['thumb_docid']);
	                $info['headimgurl'] = $pic[0]['img_url'];
	            }
	        }
	        
	        return $info;
	    }
	    
	    //获取支付信息
	    public function saveOrder($da,$patDetail)
	    {
	        

	        $code       = 900;
	        $uid        = intval($this->_user['uid']);
	        
	        $donext     = true;
	        
	 
	        if ($da && $patDetail['pay_down_time']>time() && $uid > 0 && empty($patDetail['order_id'])) {
	        
	           
	        
	            // 订单拆分
	            if ($patDetail) {
	        
	                //地址相关信息
	                $address_id = intval($da['address_id']);
	                if ($address_id > 0) {
	                    $addr = $this->getAddress($uid, $address_id);
	                }
	        
	                $order_field['goods_amount'] = $order_field['order_amount'] = $order_field['supply_amount'] = $order_field['purchase_amount'] = $order_field['shipping_fee'] = $order_field['pay_fee'] = 0;
	        
	                //订单编号
	                $order_sn                = $this->makeOrderSn();
	                $order_field['order_sn'] = $order_sn;
	                $order_field['uid']      = $uid;
	                $order_field['add_time'] = $_SERVER['REQUEST_TIME'];
	                $order_field['remark']   = addslashes(stripslashes(trim($da['remark'])));
	                // 支付类型微信公众号
	                $order_field['transaction_type'] = 1;
	        
	                if ($addr) {
	        
	                    $order_field['idnum']       = stripslashes($addr['idnum']);
	                    $order_field['consignee']   = stripslashes($addr['realname']);
	                    $order_field['mobile']      = stripslashes($addr['mobile']);
	                    $order_field['address']     = stripslashes($addr['address']);
	                    $order_field['province_id'] = $addr['province_id'];
	                    $order_field['city_id']     = $addr['city_id'];
	                    $order_field['county_id']   = $addr['county_id'];
	        
	                } else {
	                    // 收货人信息不完整
	                    $code   = 850;
	                    $donext = false;
	                }
	        
	                if ($donext) {
	                    
	                    $order_field['goods_amount'] = $patDetail['bid_price'];
	                    if ($order_field['goods_amount'] > 0) {
	        
	                        //开始事务
	                        $this->db->begin();
	                        $rollback                    = true;
	                        $code                        = 403;
	                        $done                        = false;
	                        $use_coupon                  = false;
	                        $order_field['discount_fee'] = 0;
	        
	                        // 运费
	                        $member_data = $this->getMemberInfo();
	                        $coupon_id   = intval($da['coupon_id']);
	                        //判断是否是会员或已经到期，会员免运费
	                        if ($member_data['member_level'] >= 1) {
	        
	                            $order_field['shipping_fee'] = 0;
	                            // 判断是否选择了免运费优惠券
	                            if ($coupon_id > 0) {
	                                $coupons = $this->getCoupon(2);
	                                if ($coupons[$coupon_id] && $order_field['goods_amount'] >= $coupons[$coupon_id]['need_money']) {
	                                    $order_field['discount_fee'] = $coupons[$coupon_id]['money'];
	                                    $use_coupon                  = true;
	        
	                                }
	                            }
	                        } else {
	        
	                            $order_field['shipping_fee'] = 8;
	        
	                            // 判断是否选择了免运费优惠券
	                            if ($coupon_id > 0) {
	                                $coupons = $this->getCoupon();
	                                if ($coupons[$coupon_id]) {
	                                    $order_field['shipping_fee'] = 0;
	                                    $use_coupon                  = true;
	        
	                                }
	                            }
	        
	                        }
	                        if($patDetail['is_free_shipping']) {
	        
	                            $order_field['shipping_fee'] = 8;
	                        }
	                        //销售类型
	                         $order_field['sell_type']  = 4;
	                        // 订单总金额
	                        $order_field['order_amount'] = $order_field['goods_amount'] + $order_field['shipping_fee'] - $order_field['discount_fee'];
	                        // 应付款金额
	                        $order_field['pay_fee']       = $pay_fee       = $order_field['order_amount'];
	                        $order_field['pay_total_fee'] = $pay_total_fee = ceil($pay_fee * 100);
	                       
	                        //上级ID
	                        if ($member_data['pid'] > 0) {
	                            $order_field['pid'] = $member_data['pid'];
	                        
	                        }
	                        // print_r($order_field);die;
	                        //组织订单SQL
	                        $sql = "INSERT INTO {$this->_prefix['primary']}order_info ";
	                        foreach ($order_field as $k => $r) {
	                            $field[] = "`" . $k . "`";
	                            $value[] = "'" . $r . "'";
	                        }
	                        $sql .= "(" . implode(",", $field) . ") VALUES (" . implode(",", $value) . ")";
	                        
	                        $order_id = $this->sql($sql);
	                        
	                        //如果插入订单成功
	                        if ($order_id > 0) {
	                        
	                            if ($use_coupon && $coupon_id > 0) {
	                                // 更新优惠券使用
	                                $sql  = "UPDATE {$this->_prefix['primary']}discount_coupon SET `order_id`=" . $order_id . ",`status`=1,`used_time`=" . $_SERVER['REQUEST_TIME'] . " WHERE `coupon_id`=" . $coupon_id . " AND `uid`=" . $this->_user['uid'];
	                                $upcc = $this->sql($sql);
	                                if ($upcc > 0) {
	                                    $done = true;
	                                } else {
	                                    $code = 858;
	                                }
	                            } else {
	                        
	                                $done = true;
	                            }
	                        
	                            if ($done) {
	                                
	                        
                                    $done = false;
                    
                                    $sql = "INSERT INTO {$this->_prefix['primary']}order_goods (
                                    `order_id`,
                                    `warehouse_type`,
                                    `goods_id`,
                                    `supplierid`,
                                    `goods_name`,
                                    `goods_sn`,
                                    `goods_barcode`,
                                    `goods_number`,
                                    `market_price`,
                                    `goods_price`,
                                    `purchase_price`,
                                    `supply_price`,
                                   
                                    `add_time`
                                    ) VALUES (
                                    " . $order_id . ",
                             " . $patDetail['warehouse_type'] . ",
                            " . $patDetail['goods_id'] . ",
                            " . $patDetail['supplierid'] . ",
                            '" . $patDetail['goods_name'] . "',
                            '" . $patDetail['goods_sn'] . "',
                            '" . $patDetail['goods_barcode'] . "',
                             1,
                            " . $patDetail['market_price'] . ",
                            " . $patDetail['bid_price'] . ",
                            " . $patDetail['purchase_price'] . ",
                            " . $patDetail['supply_price'] . ",
                          
                            " . $_SERVER['REQUEST_TIME'] . "
                            )";
                    
                                    $rec_id = $this->sql($sql);
                    
                                    if ($rec_id > 0) {
                                        
                    
                                        //更新商品库存
                                       /*  $sql     = "UPDATE {$this->_prefix['primary']}goods_stock SET `number`=`number`-1 WHERE `goods_id`=" .$patDetail['goods_id'] . " AND `number`>=1";
                                        $rcstock = $this->sql($sql); */
                                        //if ($rcstock > 0) {
                    
                                            // 更新商品销售数量
                                            $sql = "UPDATE {$this->_prefix['primary']}goods_data SET `sales_count`=`sales_count`+1  WHERE `goods_id`=" . $patDetail['goods_id'];
                                            $rcg = $this->sql($sql);
                                            if ($rcg > 0) {
                                                //出价表插入订单ID
                                                $sql = "UPDATE {$this->_prefix['primary']}order_bidrecord SET `order_id`=".$order_id." WHERE `id`=" . $patDetail['id'];
                                                $ob = $this->sql($sql);
                                                
                                                if($ob){
                                                    $done = true;
                                                
                                                }else{
                                                    $code = 858;
                                                    
                                                }
                    
                                            } else {
                    
                                                $code = 857;
                                            }
                    
                                       /*  } else {
                                            $code = 856;
                    
                                        } */
                    
                                    } else {
                                        $code = 855;
                                    }
	                        
	                            }
	                        
	                        } else {
	                            $code = 853;
	                        }
	                         
	        
	                        //执行COMMIT
	                        if ($done) {
	                            $code = 200;
	                            $this->db->commit();
	        
	                        } else {
	                            $this->db->rollback();
	                        }
	        
	                    } else {
	                        $code = 852;
	                    }
	                }
	        
	            }
	        }
	        
	        return array(
	            'code'          => $code,
	            'order_id'      => $order_id,
	            'order_sn'      => $order_sn,
	            'pay_fee'       => $pay_fee,
	            'pay_total_fee' => $pay_total_fee,
	        );
	    }
	    // 获取优惠券
	    public function getCoupon($type = 0)
	    {
	        if ($type > 0) {
	            $tsql = " AND `type`=" . intval($type);
	        }
	    
	        $sql  = "SELECT * FROM {$this->_prefix['primary']}discount_coupon WHERE `uid`=" . $this->_user['uid'] . $tsql . " AND `status`=0 AND `expire_time`>" . $_SERVER['REQUEST_TIME'] . " ORDER BY `expire_time` DESC";
	        $data = $this->sql($sql);
	        if ($data) {
	            foreach ($data as $r) {
	                $new[$r['coupon_id']] = $r;
	            }
	    
	            return $new;
	        }
	    }
	    // 制作订单编号
	    public function makeOrderSn()
	    {
	        $sql           = "SELECT `order_id` FROM {$this->_prefix['primary']}order_info ORDER BY `order_id` DESC LIMIT 1";
	        $prev_order_id = $this->db->getone($sql);
	        if (!$prev_order_id || $prev_order_id < 1) {
	            $prev_order_id = 1;
	        }
	    
	        $order_sn = date("Ymd") . rand(100, 999) . ($prev_order_id + 1);
	    
	        return $order_sn;
	    } 
	    
	    //获取图片
	    public function getPic($docid, $type = false)
	    {
	        if (is_array($docid)) {
	            $where = " WHERE `docid` IN (" . implode(",", array_unique($docid)) . ")";
	        } else {
	            if (!$type) {
	                $where = " WHERE `docid`=" . intval($docid);
	            } else {
	    
	                $where = " WHERE `docid` IN (" . $docid . ")";
	            }
	    
	        }
	    
	        $sql = "SELECT `docid`,`filename`,`create_time`,imgwh FROM {$this->_prefix['primary']}documents " . $where;
	        $rs  = $this->sql($sql);
	        if ($rs) {
	            foreach ($rs as $k => $r) {
	                $doc[$k]['img_url'] = $this->c['doc_host'] . date("Ymd", $r['create_time']) . "/" . date("H", $r['create_time']) . "/" . date("i", $r['create_time']) . "/" . $r['filename'];
	                $doc[$k]['imgwh']   = $r['imgwh'];
	            }
	            return $doc;
	        } else {
	            return false;
	        }
	    
	    }
	    
	    //获取你问我答公告
	    public function getService()
	    {
	        $sql = "SELECT * FROM {$this->_prefix['primary']}adv WHERE `position`=7 AND `status`=1 ORDER BY `id` DESC";
	        $data = $this->db->getrow($sql);
	        return $data;
	    }
	    //获取收货地址
	    public function getAddress($uid, $id = 0, $limit = false)
	    {
	    
	        if ($id > 0) {
	            $tsql = " AND `id`=" . $id;
	        }
	    
	        if ($limit) {
	            $limit_str = ' LIMIT 1';
	        }
	    
	        $sql = "SELECT * FROM {$this->_prefix['primary']}member_address WHERE `uid`=" . $this->_user['uid'] . $tsql . " ORDER BY `is_default` DESC" . $limit_str;
	        if ($id > 0 || $limit) {
	            $data = $this->db->getrow($sql);
	            if ($data) {
	                if ($data['province_id'] > 0) {
	                    $region_id[] = $data['province_id'];
	                }
	    
	                if ($data['city_id'] > 0) {
	                    $region_id[] = $data['city_id'];
	                }
	    
	                if ($data['county_id'] > 0) {
	                    $region_id[] = $data['county_id'];
	                }
	    
	                if ($region_id) {
	                    $sql         = "SELECT `region_id`,`region_name` FROM {$this->_prefix['primary']}region WHERE `region_id` IN (" . implode(",", array_unique($region_id)) . ")";
	                    $region_data = $this->sql($sql);
	                    if ($region_data) {
	                        foreach ($region_data as $r) {
	                            $region[$r['region_id']] = $r['region_name'];
	                        }
	    
	                        if ($data['province_id'] > 0) {
	                            $data['province_name'] = $region[$data['province_id']];
	                        }
	    
	                        if ($data['city_id'] > 0) {
	                            $data['city_name'] = $region[$data['city_id']];
	                        }
	    
	                        if ($data['county_id'] > 0) {
	                            $data['county_name'] = $region[$data['county_id']];
	                        }
	                    }
	                }
	            }
	        } else {
	            $data = $this->sql($sql);
	            if ($data) {
	                foreach ($data as $r) {
	                    if ($r['province_id'] > 0) {
	                        $region_id[] = $r['province_id'];
	                    }
	    
	                    if ($r['city_id'] > 0) {
	                        $region_id[] = $r['city_id'];
	                    }
	    
	                    if ($r['county_id'] > 0) {
	                        $region_id[] = $r['county_id'];
	                    }
	    
	                }
	    
	                if ($region_id) {
	                    $sql         = "SELECT `region_id`,`region_name` FROM {$this->_prefix['primary']}region WHERE `region_id` IN (" . implode(",", array_unique($region_id)) . ")";
	                    $region_data = $this->sql($sql);
	                    if ($region_data) {
	                        foreach ($region_data as $r) {
	                            $region[$r['region_id']] = $r['region_name'];
	                        }
	                    }
	    
	                    foreach ($data as $k => $r) {
	                        if ($r['province_id'] > 0) {
	                            $data[$k]['province_name'] = $region[$r['province_id']];
	                        }
	    
	                        if ($r['city_id'] > 0) {
	                            $data[$k]['city_name'] = $region[$r['city_id']];
	                        }
	    
	                        if ($r['county_id'] > 0) {
	                            $data[$k]['county_name'] = $region[$r['county_id']];
	                        }
	    
	                    }
	                }
	            }
	        }
	        return $data;
	    }
	    //保存提现申请
	    public function doWithdraw($da)
	    {
	    
	        $code = 900;
	        if ($da['money'] != "") {
	            $money = number_format($da['money'], 2, ".", "");
	        } else {
	            $money = 0;
	        }
	        if($da['withdraw_type']==2){
	            if ($da['bank']) {
	                $sql  = "select * from {$this->_prefix['primary']}member_bank_card where id={$da['bank']} and uid=" . $this->_user['uid'];
	                $info = $this->db->getrow($sql);
	                $user = $this->getMemberInfo();
	                if ($info) {
	                    $realname     = $info['account_name'];
	                    $bank_sn      = $this->c['bank'][$info['bank_id']];
	                    $bank_account = $info['card_number'];
	                    $mobile       = $user['mobile'];
	    
	                }
	    
	            }
	        }
	        $getSetting    = $this->getSetting();
	        $less_withdraw = $getSetting['min_coins_money'];
	    
	        if ($money >= $less_withdraw) {
	    
	            $balance = $this->getMemberMoney();
	            if ($balance['coins_money'] >= $money) {
	    
	                $charge_fee = number_format(($money * $getSetting['coins_percent']), 2, ".", "");
	                if($getSetting['min_coins_charge'] >0 && $charge_fee <$getSetting['min_coins_charge'] )
	                {
	                    $charge_fee = $getSetting['min_coins_charge'];
	                }
	    
	                $pay_money  = number_format(($money - $charge_fee), 2, ".", "");
	                //开启事务
	                $this->db->begin();
	                $commit = false;
	                $code   = 403;
	                //$note          = '申请提现' . $money . '元到'.$realname."[".$bank_sn.$bank_account."]";
	                //保存提现信息
	                $withdraw_type = intval($da['withdraw_type']);
	    
	                if($withdraw_type==2){
	                    $sql  = "INSERT INTO {$this->_prefix['primary']}finance_withdraw (`uid`,`money`,`realname`,`mobile`,`bank_sn`,`bank_account`,`charge_fee`,`create_time`,`withdraw_type`,`coin_type`) VALUES (" . $this->_user['uid'] . "," . $pay_money . ",'" . $realname . "','" . $mobile . "','" . $bank_sn . "','" . $bank_account . "','" . $charge_fee . "','" . $_SERVER['REQUEST_TIME'] . "'," . $withdraw_type . ",2)";
	                    $txid = $this->sql($sql);
	                    $detail_note = '九云币申请提现' . $money . '元到' . $realname . '[' . $bank_sn . $bank_account . ']，冻结' . $money . '元';
	                }else{
	                    $detail_note = '申请提现' . $money . '元到微信零钱，冻结' . $money . '元';
	                    $sql  = "INSERT INTO {$this->_prefix['primary']}finance_withdraw (`uid`,`money`,`create_time`,`withdraw_type`,`charge_fee`,`coin_type`) VALUES (" . $this->_user['uid'] . "," . $pay_money . ",'" . $_SERVER['REQUEST_TIME'] . "'," . $withdraw_type . ",'" . $charge_fee . "',2)";
	                    $txid = $this->sql($sql);
	                }
	               
	                 
	                if ($txid > 0) {
	    
	                    //扣除用户资金
	                    $sql = "UPDATE {$this->_prefix['primary']}finance_balance SET `coins_money`=`coins_money`-" . $money . ",`freeze_coins_money`=`freeze_coins_money`+" . $money . ",`last_edit`=" . $_SERVER['REQUEST_TIME'] . ",`update_time`=" . $_SERVER['REQUEST_TIME'] . ",`last_withdraw`=" . $_SERVER['REQUEST_TIME'] . ",`random_num`=" . rand(100000, 999999) . " WHERE `uid`=" . $this->_user['uid'];
	    
	                    $baup = $this->sql($sql);
	                    if ($baup > 0) {
	    
	                        //保存资金明细
	                         
	                        $event       = 4;
	                        $sql         = "INSERT INTO {$this->_prefix['primary']}finance_coins (`uid`,`event`,`cmoney`,`money`,`freeze`,`dataid`,`table_name`,`create_time`,`note`) VALUES (" . $this->_user['uid'] . "," . $event . "," . $money . "," . ($balance['coins_money'] - $money) . "," . ($balance['freeze_coins_money'] + $money) . "," . $txid . ",'finance_withdraw'," . $_SERVER['REQUEST_TIME'] . ",'" . $detail_note . "')";
	                        $detail_id   = $this->sql($sql);
	    
	                        if ($detail_id > 0) {
	    
	                            $code   = 200;
	                            $commit = true;
	    
	                        } else {
	                            $code = 411;
	                        }
	    
	                    } else {
	                        $code = 410;
	                    }
	    
	                } else {
	                    $code = 409;
	                }
	    
	                if ($commit) {
	                    $this->db->commit();
	                } else {
	                    $this->db->rollback();
	                }
	    
	            } else {
	                $code = 408;
	            }
	    
	        } else {
	            $code = 407;
	        }
	    
	        return array(
	            'code' => $code,
	        );
	    }
	    
	 
}